<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>dkp.lua import into wowdkper</title>
</head>
<?php
/**
+--------------------------------------------------------------------------------------------------+
* @autor tonera <tonera at gmail.com>;
* @since 2006-8-5
* @version $Id: dkptable2wowdkper.php,v 1.3.2 tonera$
* @description	dkptable数据转换到wowdkper1.3以上版本.转换前请先将数据文件dkp.lua放在此程序所在目录下
+--------------------------------------------------------------------------------------------------+
*/
set_time_limit(0);
error_reporting(0);

if(file_exists("./php/config.inc.php")) {
	$cfile	= "./php/config.inc.php";
}elseif(file_exists("../php/config.inc.php")) {
	$cfile	= "../php/config.inc.php";
}else {
	echo "请先安装wowdkper 1.3或以上版本.";
}
require_once $cfile;

include_once(CHINO_PATH . "/Chino.php");
require_once CHINO_MODPATH.'/config/config.inc.php';
$GLOBALS['adodb'] = Chino::getObject('adodb');
$GLOBALS['adodb']->debug	= false;
include_once CHINO_LIBPATH.'/db_mysql.inc.php';

$etrs	= $GLOBALS['adodb']->execute("select * from ".TABLEHEAD."_eventtype");
while(!$etrs->EOF) {
	$etArray[$etrs->fields['id']]	= $etrs->fields['name'];
	$etrs->MoveNext();
}
$dkpfile	= "./dkp.lua";
if(!file_exists($dkpfile)){
	exit("请先下载dkp.lua文件");
}


if(!empty($_POST['submit'])) {
	$con	= file_get_contents($dkpfile);
	$con	= str_replace("\n","",$con);
	$con	= str_replace("\r\n","",$con);
	$con	= str_replace("\r","",$con);
	$con	= str_replace("\t","",$con);
	

	$pattern	= "/(DKP_tables\[[1-9]+\]).*\{\s*(\{.*\}),?\s*\}\s*;\s*\\1.title\s*\=\s*\"([^;]*)\";/i";
	$newcon		= preg_match_all($pattern, $con, $arrays,PREG_SET_ORDER);

	$etid		= (int)$_POST['etid'];
	$raidtime	= date("Y-m-d");
	$regtime	= date("Y-m-d H:i:s");

	//class and group
	$grouparray	= $classarray = array();
	$sql		= "select * from ".TABLEHEAD."_group";
	$grs		= $GLOBALS['adodb']->execute($sql);
	while(!$grs->EOF){
		$grouparray[$grs->fields['id']]	= $grs->fields['name'];
		$grs->MoveNext();
	}
	$sql		= "select * from ".TABLEHEAD."_work";
	$wrs		= $GLOBALS['adodb']->execute($sql);
	while(!$wrs->EOF){
		$classarray[$wrs->fields['id']]	= $wrs->fields['name'];
		$wrs->MoveNext();
	}
	$workarray	= array('rogue'=>'盗贼','warrior'=>'战士','mage'=>'法师','priest'=>'牧师','druid'=>'德鲁伊','hunter'=>'猎人','warlock'=>'术士','shaman'=>'萨满','paladin'=>'圣骑士');

	if(!is_array($arrays)){
		echo "Error:无法解析dkp.lua文件，请访问http://www.iboko.net了解更多内容。";
	}else{
		foreach($arrays as $key=>$val){
			$userinfo	= array();
			$con		= $val['2'];
			preg_match_all("/\{\s*nick\s*=\s*\"?([^\}|^\"]*)\"?\s*,\s*(rank\s*=\s*\"?([^\}|^\"]*)\"?\s*,)*\s*class\s*=\s*([^\}]*),\s*current\s*=\s*([^\}]*),\s*spent\s*=\s*([^\}]*)\s*},+/i",$con,$userinfo,PREG_SET_ORDER);

			//寻找副本是否存在，不存在则创建新副本，取得新副本id,存在则直接取得副本id->创建一个调节事件->取得事件id->给会员进行调节
			$copyname	= addslashes(trim($val[3]));
			$sql		= "select id from ".TABLEHEAD."_copy where name='$copyname'";
			$crs		= $GLOBALS['adodb']->SelectLimit($sql,1);
			$cid		= $crs->fields['id'];
			
			if(empty($cid)){
				$sql	= "insert into ".TABLEHEAD."_copy(name) values('$copyname')";
				$GLOBALS['adodb']->execute($sql);
				$cid	= $GLOBALS['adodb']->Insert_ID();
			}
			//create event
			$sql	= "insert into ".TABLEHEAD."_event(name,etid,raidtime,cid) values('Old Data','$etid','$raidtime','$cid')";

			$GLOBALS['adodb']->execute($sql);
			$eid	= $GLOBALS['adodb']->Insert_ID();
			//member info
			if(is_array($userinfo)){
				foreach($userinfo as $key1=>$val1){
					$username	= $val1[1];
					$userdkp	= (float)$val1[5];
					$classarr	= explode('_',$val1[4]);
					$userclass	= $workarray[trim($classarr[1])];
					$workid		= array_search($userclass,$classarray);
					$groupid	= array_search($val1[3],$grouparray);
					//查找会员是否存在:如果不存在，写入，否则取id
					$sql	= "select id from ".TABLEHEAD."_user where name='$username'";
					$urs	= $GLOBALS['adodb']->SelectLimit($sql,1);
					$uid	= $urs->fields['id'];
					if(empty($uid)){
						$sql	= "insert into ".TABLEHEAD."_user(name,workid,groupid,stat,regtime,lastraidtime) values('$username','$workid','$groupid','1','$regtime','$regtime')";
						$GLOBALS['adodb']->execute($sql);
						$uid	= $GLOBALS['adodb']->Insert_ID();
					}
					//调节记录
					$sql	= "insert into ".TABLEHEAD."_itemdis(eid,uid,value,distime,stat,cid) values('$eid','$uid','$userdkp','$regtime','1','$cid')";
					$GLOBALS['adodb']->execute($sql);
					//如果用户此副本dkp中有记录，则更新。无则插入
					$sql	= "select uid from ".TABLEHEAD."_dkpvalues where uid='$uid' and copyid='$cid'";
					$ucrs	= $GLOBALS['adodb']->execute($sql);
					if(empty($ucrs->fields['uid'])){
						$sql	= "insert into ".TABLEHEAD."_dkpvalues(dkpvalue,uid,copyid,raidnum) values('$userdkp','$uid','$cid','1')";
					}else{
						$sql	= "update ".TABLEHEAD."_dkpvalues set dkpvalue=dkpvalue+$userdkp where uid='$uid' and copyid='$cid'";
					}
					$ers		= $GLOBALS['adodb']->execute($sql);
				}
			}
		}		
	}
	if($ers){
		echo "<font color=red>导入完成，请删除此文件和dkp.lua文件.不要多次导入.[OK,finished. Please delete the file]</font>";
	}
}


?>

<body>
<form name="form1" method="post" action="?">
<table width="100%" border="0">
  <tr class=""> 
    <th colspan="2">从DKPTABLE数据文件dkp.lua导入数据到WOW-DKPer[Import dkp.lua into wowdkper]</th>
  </tr>
  <tr> 
    <td>请选择事件类型[Select type of event]：</td>
    <td>
	<select name="etid">
	<?php 
	$etArray	= is_array($etArray)?$etArray:array();
	foreach($etArray as $key=>$val) {
		echo("<option value=$key>$val</option>");
	}
	?>
    </select></td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td><input name="submit" type="submit" id="submit"></td>
  </tr>
</table>
</body>
</form>
</html>


